package cn.neusoft.controller;

import cn.neusoft.domain.Warehouse;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.service.WarehouseService;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/warehouseManage", produces = "application/json;charset=utf-8")
@Api(value = "仓库管理模块的api")
public class WarehouseController {
    @Autowired
    WarehouseService warehouseService;


    @PostMapping("/add")
    @ApiOperation(value = "添加")
    public ResultBean<Warehouse> add(@RequestBody Warehouse warehouse){
        ResultBean resultBean = new ResultBean();
        if(warehouse==null){
            resultBean.setMsg("空信息");
        }else {
            warehouseService.add(warehouse);
            resultBean.setMsg("添加成功");
        }
        return resultBean;
    }
    @GetMapping("/{id}")
    public ResultBean selectById(@PathVariable("id") Long id){
        Warehouse warehouse = warehouseService.selectById(id);
        if(warehouse != null){
            return ResultBean.success(warehouse);
        }
        return ResultBean.error();
    }
    @GetMapping("/all")
    public ResultBean getAll(){
        List<Warehouse> warehouses =  warehouseService.selectAll();
        return ResultBean.success(warehouses);
    }
    @PostMapping("/update")
    public ResultBean update(@RequestBody Warehouse warehouse){
        if(warehouse == null){
            return ResultBean.error(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        int i = warehouseService.update(warehouse);
        return ResultBean.success(i);
    }
    @PostMapping("/delete")
    public ResultBean DeleteById(@RequestBody Long id){
        int i = warehouseService.DeleteById(id);
        if(i == 1){
            return ResultBean.success();
        }
        return ResultBean.error();

    }
    @GetMapping("/getWarehouseById/{id}")
    public ResultBean getWarehouseById(@PathVariable("id") Long id){
        Warehouse warehouse = warehouseService.selectById(id);
        if(warehouse == null){
            return ResultBean.error(AppExceptionCodeMsg.DEPT_NOT_EXISTS);
        }
        return ResultBean.success(warehouse);
    }

}
